package com.lovo.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MQSConfig {

    //spring封装rabiitmq的代码 RabbitTemplate
//注入工厂（已经预加载）
    @Autowired
    CachingConnectionFactory connectionFactory;

    //rabbitmq模板
    @Bean(value = "rabbitTemplate")
    public RabbitTemplate rabbitTemplate() {
        return new RabbitTemplate(connectionFactory);
    }

    //招标 队列
    @Bean
    public Queue tenderQueue() {

        return new Queue("tenderQueue");
    }

    @Bean
    public Queue GoodsQueue() {

        return new Queue("GoodsQueue");
    }

    //招标 交换机
    @Bean
    public DirectExchange directExchangeTender() {

        return new DirectExchange("directExchangeTender");
    }

    //招标 交换机
    @Bean
    public DirectExchange directExchangeGoods() {

        return new DirectExchange("directExchangeGoods");
    }

    // 招标 交换机与队列进行绑定
    @Bean
    public Binding tenderQueueToDirectExchangeTender(Queue tenderQueue, DirectExchange directExchangeTender) {

        return BindingBuilder.bind(tenderQueue).to(directExchangeTender).with("tender.key");
    }


    // 招标 交换机与队列进行绑定
    @Bean
    public Binding GoodsQueueToDirectExchangeGoods(Queue GoodsQueue, DirectExchange directExchangeGoods) {

        return BindingBuilder.bind(GoodsQueue).to(directExchangeGoods).with("goods.key");
    }

}
